# -*- coding: utf-8 -*-
# standard
import os
import sys
from typing import List

# third
import psutil

# local
_P_PATH =  os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
if _P_PATH not in sys.path:
    sys.path.append(_P_PATH)


"""
@Title:   
@File: kill掉旧的进程.py
@Author: walle 2025年01日20日 17时25分26秒
@Version: 1.0.0
@Desc: 
"""


def clear_all_processes(cmd_line: str | None = None):
    """
    kill 掉指定目录下的进程
    :param path: _description_, defaults to None
    """
    cmd_line = cmd_line if cmd_line else "/home/task-center/venv/bin/python"
    names = [
        "ppid", "pid", "name", "exe", "cmdline", "cwd"
    ]
    for proc in psutil.process_iter(names):
        info = proc.as_dict()
        if info['cmdline'] and cmd_line in info['cmdline']:
            # 效果不佳，请使用killall python 和 kill -9 来结束进程
            # proc.kill()
            ms = f'进程{info["name"]}, pid: {info["pid"]}, exe: {info["exe"]}'
            print(ms)


if __name__ == '__main__':
    # clear_all_processes()
    pass